var grid;
function initGrid(authority){
	grid = $("#maingrid").ligerGrid({
		url:HTR.U('index/stocks/pageQuery'),
		pageSize:HTR.pageSize,
		pageSizeOptions:HTR.pageSizeOptions,
		height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
	        { display: '产品名称', name: 'goodsName',align:'left',isSort: false},
	        { display: '店铺名称', name: 'shopName',align:'left',isSort: false,width:170},
	        { display: '区域', name: 'areaPathName',align:'left',isSort: false,width:170},
	        { display: '成本价格', name: 'costPrice',isSort: false,width:70},
	        { display: '销售价格', name: 'salePrice',isSort: false,width:70},
	        { display: '利润', name: 'profit',isSort: false,width:70},
	        { display: '单位', name: 'goodsUnit',isSort: false,width:70},
	        { display: '库存', name: 'goodsStock',isSort: false,width:70,render: function (rowdata, rowindex, value){
	            return "<a href='" + HTR.U('index/stocklog/index','shopId=' + rowdata['shopId'] + '&goodsId=' + rowdata['goodsId'])+"'>"+rowdata['goodsStock']+"</a> "; 
	        }},
	        { display: '销量', name: 'saleNum',isSort: false,width:70},
	        { display: '操作', name: 'op',isSort: false,width:120,render: function (rowdata, rowindex, value){
	            var h = "";
	            if(HTR.GRANT.KCGL_02 && authority == 1)h += "<a href='javascript:toEdit(" + rowdata['stockId'] + ")'>编辑</a> ";
	            if(HTR.GRANT.KCGL_02 && authority == 1)h += "<a href='javascript:editStock(" + rowdata['shopId'] + "," + rowdata['goodsId'] + ")'>库存</a> ";
	            if(HTR.GRANT.KCGL_03 && authority == 1)h += "<a href='javascript:del(" + rowdata['stockId'] + ")'>删除</a> "; 
	            return h;
	        }}
        ]
    });
}
function loadGrid(){
	var params = HTR.getParams('.j-ipt');
	params.areaIdPath = HTR.ITGetAllAreaVals('areaId1','j-areas').join('_');
	grid.set('url',HTR.U('index/stocks/pageQuery',params));
}

function del(id){
	var box = HTR.confirm({content:"您确定要删除该库存吗?",yes:function(){
   var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
   $.post(HTR.U('index/stocks/del'),{id:id},function(data,textStatus){
   			layer.close(loading);
   			var json = HTR.toIndexJson(data);
   			if(json.status=='1'){
   			    HTR.msg(json.msg,{icon:1});
   			    layer.close(box);
   			    grid.reload();
   			}else{
   			    HTR.msg(json.msg,{icon:2});
   			}
   		});
    }});
}
function toEdit(id){
	location.href=HTR.U('index/stocks/toEdit','id='+id);
}
function save(){
	$('#editFrom').isValid(function(v){
		if(v){
			var params = HTR.getParams('.ipt');
			var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
		    $.post(HTR.U('index/stocks/'+((params.stockId==0)?"add":"edit")),params,function(data,textStatus){
		    	layer.close(loading);
		    	var json = HTR.toIndexJson(data);
		    	if(json.status=='1'){
		    		HTR.msg("操作成功",{icon:1});
		    		location.href=HTR.U('index/stocks/index');
		    	}else{
		    		HTR.msg(json.msg,{icon:2});
		    	}
		    });
		}
	});
}


function editStock(shopId,goodsId){
	$('#stockForm')[0].reset();
	var box = HTR.open({title:"调整库存",type:1,content:$('#stockBox'),area: ['460px', '260px'],btn:['确定','取消'],yes:function(){
		$('#stockForm').submit();
	          }});
	$('#stockForm').validator({
	    fields: {
	    	num: {
	    		tip: "请输入数量",
	    		rule: '数量:required;'
	    	},
		    desc: {
	    		tip: "请输入描述",
	    		rule: '描述:required;'
	    	},
	    },
	    valid: function(form){
	        var params = HTR.getParams('.ipt');
	        params.shopId = shopId;
	        params.goodsId = goodsId;
	        var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
	    		$.post(HTR.U('index/stocks/upStock'),params,function(data,textStatus){
	    			  layer.close(loading);
	    			  var json = HTR.toIndexJson(data);
	    			  if(json.status=='1'){
	    			    	HTR.msg(json.msg,{icon:1});
	    			    	layer.close(box);
	    		            grid.reload();
	    			  }else{
	    			        HTR.msg(json.msg,{icon:2});
	    			  }
	    		});
	    }
	});
}

function toScan(shopId){
	if(!shopId){
		$('#shopForm')[0].reset();
		var box = HTR.open({title:"选择店铺",type:1,content:$('#shopBox'),area: ['460px', '200px'],btn:['确定','取消'],yes:function(){
			$('#shopForm').submit();
		          }});
		$('#shopForm').validator({
		    fields: {
		    	shopId: {
		    		tip: "请选择店铺",
		    		rule: '选择店铺:required;'
		    	}
		    },
		    valid: function(form){
		        var params = HTR.getParams('.ipt');
		        var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
		    		$.post(HTR.U('index/stocks/setShop'),params,function(data,textStatus){
		    			  layer.close(loading);
		    			  var json = HTR.toIndexJson(data);
		    			  if(json.status=='1'){
		    			    	HTR.msg(json.msg,{icon:1});
		    					location.href=HTR.U('index/stocks/scan');
		    			  }else{
		    			        HTR.msg(json.msg,{icon:2});
		    			  }
		    		});
		    }
		});		
	}else{
		location.href=HTR.U('index/stocks/scan');
	}
}